<?php
namespace App\Admin\Controller;

use App\Models\SilverDollarCoin;
use Illuminate\Routing\Controller;
use ModStart\Admin\Concern\HasAdminCRUD;
use ModStart\Detail\Detail;
use ModStart\Form\Form;
use ModStart\Grid\Grid;
use ModStart\Core\Input\Response;

class SilverdollarcoinController extends Controller
{
    use HasAdminCRUD;
    public function grid()
    {
        $grid = Grid::make('silver_dollar_coin');
        $grid->id('id', 'ID');
        $grid->datetime('date_time', '日期')->defaultValue();
        $grid->decimal('price', '价格');
        $grid->select('coin_id', '名称')->options(['2'=>'龙币']);
        $grid->display('created_at', '创建时间');
        $this->pageTitle('货币管理');
        return $grid;
    }
    public function form()
    {
        $form = Form::make('silver_dollar_coin');
        $form->date('date_time', '日期')->required();
        $form->decimal('price', '金额')->required();
        $form->select('coin_id', '名称')->options(['2'=>'龙币']);
        $form->hookSaving(function (Form $form) {
            if ($form->isModeAdd()) {
                // 增加模式
                $data = $form->dataAdding();
                $n = SilverDollarCoin::where(['date_time'=>$data['date_time']])->first();
                if($n){
                    return Response::generate(-1, '该日期已经设置了龙币价格,不能重复添加!');
                }
                // 替换入库数据
                $form->dataAdding($data);
            } else if ($form->isModeEdit()) {
                // 修改
                $yuan = $form->item();
                $data = $form->dataEditing();
                if($yuan->date_time != $data['date_time']){
                    return Response::generate(-1, '日期不能修改,可以重新添加日期!');
                }
                // 替换入库数据
                $form->dataEditing($data);
            }
        });
        return $form;
    }
    public function detail()
    {
        $detail = Detail::make('silver_dollar_coin');
        $detail->id('id', 'ID');
        $detail->date('date_time', '日期');
        $detail->decimal('price', '金额');
        $detail->select('coin_id', '名称')->options(['2'=>'龙币']);
        return $detail;
    }
}